﻿using System;
using System.Configuration;
using System.Text;
using System.Data;

namespace CrazyTalk.Common
{
    /// <summary>
    /// 生成随机码枚举
    /// </summary>
    /// <remarks></remarks>
    public enum ECodeType
    {
        /// <summary>
        /// 从1-9,A-Z字符串里随机得到，规定个数的字符串.
        /// </summary>
        AZ19,
        /// <summary>
        /// 从1-9,a-z字符串里随机得到，规定个数的字符串.
        /// </summary>
        az19,
        /// <summary>
        /// 从1-9,A-Z,a-z字符串里随机得到，规定个数的字符串.
        /// </summary>
        AZaz19
    }

    /// <summary>
    /// Assistant 的摘要说明。
    /// </summary>
    /// <remarks></remarks>
    public sealed class Assistant
    {

        #region
        /// <summary>
        /// 从1-9
        /// </summary>
        /// <param name="CodeCount">The code count.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetRandomCodeByNum(int CodeCount)
        {
            string allChar = "1,2,3,4,5,6,7,8,9";

            return GetRandomCode(allChar, CodeCount);
        }
        /// <summary>
        /// 从1-9,A-Z字符串里随机得到，规定个数的字符串.
        /// </summary>
        /// <param name="CodeCount">The code count.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetRandomCode(int CodeCount)
        {
            string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";

            return GetRandomCode(allChar, CodeCount);
        }


        /// <summary>
        /// 从0-9规定个数的字符串
        /// </summary>
        /// <param name="CodeCount">The code count.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetRandomNum(int CodeCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9";

            return GetRandomCode(allChar, CodeCount);
        }


        /// <summary>
        /// 根据ECodeType从字符串里随机得到，规定个数的字符串.
        /// </summary>
        /// <param name="CodeCount">The code count.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetRandomCode(int CodeCount, ECodeType type)
        {
            int s = int.Parse(GetRandomCodeByNum(2));
            System.Threading.Thread.Sleep(s);
            string allChar = string.Empty;
            switch (type)
            {
                case ECodeType.AZ19:
                    allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
                    break;
                case ECodeType.az19:
                    allChar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
                    break;
                case ECodeType.AZaz19:
                    allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
                    break;
                default:
                    allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
                    break;
            }

            return GetRandomCode(allChar, CodeCount);
        }
        /// <summary>
        /// 从字符串里随机得到，规定个数的字符串.
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
        public static string GetRandomCode(string allChar, int CodeCount)
        {

            //string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(allCharArray.Length - 1);

                while (temp == t)
                {
                    t = rand.Next(allCharArray.Length - 1);
                }

                temp = t;
                RandomCode += allCharArray[t];
            }
            return RandomCode;
        }

        /// <summary>
        /// 从 GUID中得到指定个数的随机码.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static string GetCodeByGuid(int count)
        {
            string guid = Guid.NewGuid().ToString().Replace("-", "").ToLower();
            return GetRandomCodeRep(guid, count);
        }

        /// <summary>
        /// 从字符串里随机得到，规定个数的字符串.可以有重复
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
        public static string GetRandomCodeRep(string allChar, int CodeCount)
        {
            //string allChar = "123456789ABCDEFGHiZ";             
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(allChar.Length - 1);


                temp = t;
                RandomCode += allChar[t];
            }
            return RandomCode;
        }

        #endregion


    }
}
